﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Service.First;

namespace FoodRaidersWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]/api")]
    public class FoodArticleController : Controller
    {
        private FoodArticleService foodArticleService;

        public FoodArticleController(FoodContext context)
        {
            foodArticleService = new FoodArticleService(context);
        }

        [HttpGet("ListByPraise/all")]
        public ActionResult<List<FoodArticle>> ListByPraise()
        {
            var query = foodArticleService.ListByPraise();
            if (query == null)
                return NotFound();
            return query;
        }

        [HttpGet("ListByPraise/n={n}")]
        public ActionResult<List<FoodArticle>> ListByPraise(int n)//n代表需要返回文章的数量
        {
            var query = foodArticleService.ListByPraise(n);
            if (query == null)
                return NotFound();
            return query;
        }

        [HttpGet("ListByDateTime/all")]
        public ActionResult<List<FoodArticle>> ListByDateTime()
        {
            var query = foodArticleService.ListByDateTime();
            if (query == null)
                return NotFound();
            return query;
        }

        [HttpGet("ListByDateTime/n={n}")]
        public ActionResult<List<FoodArticle>> ListByDateTime(int n)//n代表需要返回文章的数量
        {
            var query = foodArticleService.ListByDateTime();
            if (query == null)
                return NotFound();
            return query;
        }

        [HttpGet("find/id={id}")]
        public ActionResult<FoodArticle> Find(int id)
        {
            var foodArticle = foodArticleService.Find(id);
            if (foodArticle == null)
                return NotFound();
            return foodArticle;
        }

        [HttpGet("addPraise/id={id}")]
        public void AddPraise(int id)
        {
            foodArticleService.AddPraise(id);
        }

        [HttpGet("subPraise/id={id}")]
        public void SubPraise(int id)
        {
            foodArticleService.SubPraise(id);
        }

        [HttpGet("user/id={id}")]
        public ActionResult<User> GetUser(int id)
        {
            var writer = foodArticleService.GetWriter(id);
            if (writer == null)
                return NotFound();
            return writer;
        }

        [HttpGet("addComment/id={id}&content={content}&uid={uid}")]
        public void AddComment(int id,string content,int uid)
        {
            foodArticleService.AddComment(id, content, uid);
        }
        
        [HttpGet("ListByLabel/lable={label}")]
        public ActionResult<List<FoodArticle>> ListByLabel(int label)
        {
            var query = foodArticleService.ListByLabel((Label)label);
            if (query == null)
                return NotFound();
            return query;
        }

        [HttpPost("addFoodArticle")]
        public void AddFoodArticle(FoodArticle foodArticle)
        {
            if (foodArticle == null)
                return;
            try
            {
                foodArticle.Article.DateTime = DateTime.Now;
                foodArticleService.AddFoodArticle(foodArticle);
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        
        [HttpGet("delete/id={id}")]
        public ActionResult<FoodArticle> Delete(int id)
        {
            var foodArticle = foodArticleService.Delete(id);
            if (foodArticle == null)
                return NotFound();
            return foodArticle;
        }
        
        [HttpGet("location")]
        public ActionResult<List<FoodArticle>> Location()
        {
            var query = foodArticleService.Location();
            if (query == null)
                return NotFound();
            return query;
        }
    }
}